/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = jolt_comm.h                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2015/06/02 by songbb                               */
/*******************************************************************/

#ifndef __JOLT_COMM_H_
#define __JOLT_COMM_H_

#include <sys/time.h>

#include "frlog.h"

#include "jolt_core.h"

#define APM_JOLT_DBG 0
#define APM_JOLT_ENABLE_FRLOG 0

/* 标识符变量的初始化值 */
#define APM_JOLT_FLAG_UNSET -1

/* 通用头长度 */
#define APM_JOLT_PKT_HEADER_LEN 64

/* MsgCode */
#define APM_JOLT_MSGCODE_CHECKAUTH 0x32
#define APM_JOLT_MSGCODE_DH 0x50
#define APM_JOLT_MSGCODE_ESTCON 0x64
#define APM_JOLT_MSGCODE_CHALLENGE 0x6e
#define APM_JOLT_MSGCODE_TICKET 0x78
#define APM_JOLT_MSGCODE_RECONNECT 0x82
#define APM_JOLT_MSGCODE_DATAXFER 0x8c
#define APM_JOLT_MSGCODE_CLOSE 0x96
#define APM_JOLT_MSGCODE_ERROR 0x384
#define APM_JOLT_MSGCODE_REPLY 0x01

/* JOpCode */
#define APM_JOLT_JOPCODE_BEGIN_RQST 100
#define APM_JOLT_JOPCODE_BEGIN_RESP 101
#define APM_JOLT_JOPCODE_COMMIT_RQST 102
#define APM_JOLT_JOPCODE_COMMIT_RESP 103
#define APM_JOLT_JOPCODE_ROLLBACK_RQST 104
#define APM_JOLT_JOPCODE_ROLLBACK_RESP 105
#define APM_JOLT_JOPCODE_SUSPEND_RQST 106
#define APM_JOLT_JOPCODE_SUSPEND_RESP 107
#define APM_JOLT_JOPCODE_RESUME_RQST 108
#define APM_JOLT_JOPCODE_RESUME_RESP 109
#define APM_JOLT_JOPCODE_CALL_RQST 200
#define APM_JOLT_JOPCODE_CALL_RESP 201
#define APM_JOLT_JOPCODE_ENQUEUE_RQST 202
#define APM_JOLT_JOPCODE_ENQUEUE_RESP 203
#define APM_JOLT_JOPCODE_DEQUEUE_RQST 204
#define APM_JOLT_JOPCODE_DEQUEUE_RESP 205
#define APM_JOLT_JOPCODE_CONNECT_RQST 206
#define APM_JOLT_JOPCODE_CONNECT_RESP 207
#define APM_JOLT_JOPCODE_SEND_RQST 208
#define APM_JOLT_JOPCODE_SEND_RESP 209
#define APM_JOLT_JOPCODE_RECV_RQST 210
#define APM_JOLT_JOPCODE_RECV_RESP 211
#define APM_JOLT_JOPCODE_DISCONNECT_RQST 212
#define APM_JOLT_JOPCODE_DISCONNECT_RESP 213
#define APM_JOLT_JOPCODE_SUBSCRIBE_RQST 300
#define APM_JOLT_JOPCODE_SUBSCRIBE_RESP 301
#define APM_JOLT_JOPCODE_UNSUBSCRIBE_RQST 302
#define APM_JOLT_JOPCODE_UNSUBSCRIBE_RESP 303
#define APM_JOLT_JOPCODE_NOTIFY_RQST 304
#define APM_JOLT_JOPCODE_NOTIFY_RESP 305
#define APM_JOLT_JOPCODE_POST_RQST 306
#define APM_JOLT_JOPCODE_POST_RESP 307
#define APM_JOLT_JOPCODE_UNSOL_RQST 308
#define APM_JOLT_JOPCODE_UNSOL_RESP 309
#define APM_JOLT_JOPCODE_GETCONFIG_RQST 400
#define APM_JOLT_JOPCODE_GETCONFIG_RESP 401
#define APM_JOLT_JOPCODE_LOGON_RQST 402
#define APM_JOLT_JOPCODE_LOGON_RESP 403
#define APM_JOLT_JOPCODE_LOGOFF_RQST 404
#define APM_JOLT_JOPCODE_LOGOFF_RESP 405
#define APM_JOLT_JOPCODE_GETDEF_RQST 500
#define APM_JOLT_JOPCODE_GETDEF_RESP 501
#define APM_JOLT_JOPCODE_GETREC_RQST 502
#define APM_JOLT_JOPCODE_GETREC_RESP 503

/* JMsgParm */
#define APM_JOLT_PARM_REPNAME 0x0a000001
#define APM_JOLT_PARM_REPVALUE 0x0a000002
#define APM_JOLT_PARM_REPNRECS 0x02000003
#define APM_JOLT_PARM_REPPATTERN 0x0a000004
#define APM_JOLT_PARM_ERRNO 0x02000005
#define APM_JOLT_PARM_REASON 0x0a000006
#define APM_JOLT_PARM_TIMEOUT 0x02000007
#define APM_JOLT_PARM_FLAGS 0x02000008
#define APM_JOLT_PARM_DATA 0x0e000009
#define APM_JOLT_PARM_NAME 0x0a00000a
#define APM_JOLT_PARM_VERSION 0x0000000b
#define APM_JOLT_PARM_CLIENTDATA 0x0200000c
#define APM_JOLT_PARM_USERNAME 0x0a00000d
#define APM_JOLT_PARM_USERROLE 0x0a00000e
#define APM_JOLT_PARM_PASSWD 0x0a00000f
#define APM_JOLT_PARM_APPASSWD 0x0a000010
#define APM_JOLT_PARM_GROUPNM 0x0a000011
#define APM_JOLT_PARM_TUXVERS 0x02000012
#define APM_JOLT_PARM_AUTHLEVEL 0x00000013
#define APM_JOLT_PARM_JOLTVERS 0x02000014
#define APM_JOLT_PARM_IDLE 0x02000015
#define APM_JOLT_PARM_MSGID 0x06000016
#define APM_JOLT_PARM_CORRID 0x0a000017
#define APM_JOLT_PARM_REPLYQ 0x0a000018
#define APM_JOLT_PARM_ERRORQ 0x0a000019
#define APM_JOLT_PARM_PRIORITY 0x0000001a
#define APM_JOLT_PARM_XID 0x0200001b
#define APM_JOLT_PARM_EVENT 0x0a00001c
#define APM_JOLT_PARM_FILTER 0x0a00001d
#define APM_JOLT_PARM_SID 0x0200001e
#define APM_JOLT_PARM_NUMEVENTS 0x0200001f
#define APM_JOLT_PARM_TYPE 0x02000020
#define APM_JOLT_PARM_NETMSGID 0x02000021
#define APM_JOLT_PARM_E_ERRNO 0x02000022
#define APM_JOLT_PARM_E_REASON 0x0a000023
#define APM_JOLT_PARM_BINPWD 0x06000024
#define APM_JOLT_PARM_PRINCIPALNAME 0x0a000025
#define APM_JOLT_PARM_REALM 0x0a000026
#define APM_JOLT_PARM_MBENCODING 0x0a000027
#define APM_JOLT_PARM_REQUESTVER 0x02000028

#define APM_DTS_REPORT_MODULE_NAME "dts"
#define APM_DTS_REPORT_SESSION_USERROLE APM_REPORT_KEY_SESSION"."APM_DTS_REPORT_MODULE_NAME".userrole"
#define APM_DTS_REPORT_REQ_SERVICE_NAME APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.name"
#define APM_DTS_REPORT_REQ_SERVICE_METHOD APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.method"
#define APM_DTS_REPORT_REQ_ARG_SIZE APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.arg.size"
#define APM_DTS_REPORT_REQ_ARG_DATA APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.arg."
#define APM_DTS_REPORT_RESP_ARG_SIZE APM_REPORT_KEY_RESP"."APM_DTS_REPORT_MODULE_NAME".service.return.size"
#define APM_DTS_REPORT_RESP_ARG_DATA APM_REPORT_KEY_RESP"."APM_DTS_REPORT_MODULE_NAME".service.return."

#define KRED        "\x1B[0;31m"
#define KSALMON     "\x1B[1;31m"
#define KGREEN      "\x1B[0;32m"
#define KLIMEGREEN  "\x1B[1;32m"
#define KYELLOW     "\x1B[0;33m"
#define KORANGE     "\x1B[0;34m"
#define KBLUE       "\x1B[1;34m"
#define KMAGENTA    "\x1B[0;35m"
#define KPINK       "\x1B[1;35m"
#define KCYAN       "\x1B[0;36m"
#define RESET       "\x1B[0m"

#if APM_JOLT_DBG
#define JOLT_PRINT(...) printf(__VA_ARGS__)
#define JOLT_PRINT_DETAIL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(__VA_ARGS__))
#define JOLT_PRINT_DBG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_FATAL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KORANGE),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_EMERG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KRED),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_ALERT(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KSALMON),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_CRIT(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KMAGENTA),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
#define JOLT_PRINT_ERR(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KPINK),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_WARN(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KYELLOW),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_NOTICE(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KLIMEGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
#define JOLT_PRINT_INFO(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KBLUE),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
#define PRINT_HEX_STRING(a,b,c) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),jolt_print_hex_string(b,c),printf("]\n"),\
                            printf(RESET))
#define JOLT_PRINT_PACKET_SPECIFIED(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),jolt_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

#define JOLT_PRINT_PACKET_CURRENT(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),jolt_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

#define JOLT_PRINT_PACKET(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),jolt_print_packet(b),printf("]\n"),\
                            printf(RESET))
#define JOLT_PRINT_TIMEVAL(a,b) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KGREEN),\
                            printf(" %s", a), \
                             jolt_print_timeval(b),\
                             printf(RESET))
#else
#define JOLT_PRINT(...)
#define JOLT_PRINT_DETAIL(...)
#define JOLT_PRINT_DBG(...)
#define JOLT_PRINT_FATAL(...)
#define JOLT_PRINT_EMERG(...)
#define JOLT_PRINT_ALERT(...)
#define JOLT_PRINT_CRIT(...)
#define JOLT_PRINT_ERR(...)
#define JOLT_PRINT_WARN(...)
#define JOLT_PRINT_NOTICE(...)
#define JOLT_PRINT_INFO(...)
#define PRINT_HEX_STRING(a, b, c)
#define JOLT_PRINT_PACKET_SPECIFIED(tag, packet, offset, len)
#define JOLT_PRINT_PACKET_CURRENT(a, b)
#define JOLT_PRINT_PACKET(a, b)
#define JOLT_PRINT_TIMEVAL(a, b)
#endif

#if APM_JOLT_ENABLE_FRLOG
#define JOLT_FRLOG_ERROR(...) FRLOG_ERROR_DETAIL(...)
#else
#define JOLT_FRLOG_ERROR(...)
#endif

#define JOLT_MALLOCK_ERR JOLT_PRINT_EMERG("can not malloc memory\n"); \
                          FRLOG_ERROR_DETAIL("can not malloc memory");

void jolt_free(void **pp_buffer);

void jolt_print_timeval(struct timeval st_timestamp);

void jolt_bin_to_hex_string(char *p_inbuff, int i_len, char *p_outbuff);

void jolt_print_hex_string(char *p_buff, int i_len);

void jolt_print_packet_specified(L7_jolt_packet *p_st_jolt_packet, int i_pos, int i_len);

void jolt_print_packet_current(L7_jolt_packet *p_st_jolt_packet);

void jolt_print_packet(L7_jolt_packet *p_st_jolt_packet);

int jolt_packet_is_in_fragment(L7_jolt_packet *p_st_jolt_packet);

int jolt_report_msg_data_req(Conntrack *p_st_conn
                            , L7session_jolt *p_st_sess
                            , char *p_title
                            , char *p_service_name
                            , char *p_service_method
                            , L7_jolt_packet *p_st_jolt_packet
                            , Frhash_header *p_st_hash_report);

int jolt_report_msg_data_resp(Conntrack *p_st_conn
                            , int i_errno
                            , char *p_reason
                            , L7_jolt_packet *p_st_jolt_packet
                            , Frhash_header *p_st_hash_report);

void jolt_msg_data_node_free(Frlist_node *p_st_node);

void jolt_free_stmt(L7_jolt_stmt **pp_st_jolt_stmt);

void jolt_init_stmt(L7_jolt_stmt *p_st_jolt_stmt);

#endif
